package com.java.yz.day14.SellTickets2;

import java.util.concurrent.locks.ReentrantLock;

public class Windows3 implements Runnable{
    int ticket =100;
    Object o = new Object();
    ReentrantLock lock=new ReentrantLock();
    //它的效率是比synchronized要低的

    @Override
    public void run() {
        while (true) {
            //相当于加了一把锁
            lock.lock();  //加锁
                if (ticket > 0) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "窗口正在卖 第" + (ticket--) + "张票");
                }
                lock.unlock(); //解锁
            }

    }
}

